package br.com.naybee.client.views.add.repartimento;

import br.com.naybee.client.NaybeeService;
import br.com.naybee.framework.client.componente.gwt.event.CommEvent;
import br.com.naybee.framework.client.componente.gwt.event.CommHandler;
import br.com.naybee.framework.client.componente.gwt.web.Div;
import br.com.naybee.framework.client.models.ListModel;
import br.com.naybee.framework.client.models.Model;
import br.com.naybee.framework.client.utils.NAYException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class RepartimentoFuncaoView extends Composite {

	private static RepartimentoViewUiBinder uiBinder = GWT.create(RepartimentoViewUiBinder.class);

	interface RepartimentoViewUiBinder extends UiBinder<Widget, RepartimentoFuncaoView> {
	}

	@UiField
	protected Div repartimentoFuncaoItem;

	private Model param;

	public RepartimentoFuncaoView() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public RepartimentoFuncaoView(Model param) {
		initWidget(uiBinder.createAndBindUi(this));
		this.param = param;
		init();
	}

	private void init() {
		NaybeeService naybeeService = new NaybeeService("funcoesFs", "listarFuncoes");
		naybeeService.post(this.param, new CommHandler() {
			public void onSuccess(CommEvent event) {
				carregarListaFucoesItem(event.getListModel());
			}

			public void onException(NAYException error) {
			}
		});
	}

	private void carregarListaFucoesItem(ListModel listModel) {
		for (Model model : listModel) {
			repartimentoFuncaoItem.add(new RepartimentoFuncaoItemView(model));
		}
	}

}
